'''
# -*- coding:utf-8 -*-
# @Project: DevOps_api
# @Date: 2023/3/16 - 20:03
# @Author: ysg
# @Describe: 

psut.py
'''
# !/usr/bin/python3
import os
import psutil
import datetime


def linux_monitor():
    # cpu的使用率
    cup_per = psutil.cpu_percent()
    # 内存使用率
    mem_per = psutil.virtual_memory().percent
    # 磁盘使用率
    disk_per = psutil.disk_usage('/').percent
    # 网络使用情况  收发多少数据 net.bytes_recv、net.bytes_sent
    net = psutil.net_io_counters()
    # 获取当前系统时间
    current_time = datetime.datetime.now().strftime("%F %T")
    # 拼接显示

    # str = ""
    # str += "|---------time--------|---cpu---|----memory----|----disk----|--------------net-------------|\n"
    # str += "| %s |   %s%%  |    %s%%     |    %s%%   | recv:%.2fMB  sent:%.2fMB |\n" \
    #        % (current_time, cup_per, mem_per, disk_per, net.bytes_recv / 1024 / 1024, net.bytes_sent / 1024 / 1024)
    info_dict = {'time': current_time, 'cpu': cup_per, 'memory': mem_per, 'disk': disk_per,
                 'recvMB': round(net.bytes_recv / 1024 / 1024), 'sentMB': round(net.bytes_sent / 1024 / 1024)}
    print(info_dict)


linux_monitor()
